package com.xiaoyu.lanling.router.deeplink;

import android.app.Activity;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xiaoyu.base.utils.ClassUtil;
import com.xiaoyu.lanling.common.config.HttpConfig;
import com.xiaoyu.lanling.feature.web.activity.WebActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.request.JsonData;
import in.srain.cube.util.UriUtil;
import in.srain.cube.util.StringUtil;

class DeepLinkLoader {

    private static final Map<Uri, ConfigItem> TABLE = new HashMap<>();
    private static final Map<Uri, ConfigItem> WEEX_TABLE = new HashMap<>();

    void updateWeexNotNeedLoginConfig(String url) { // temp
        Uri baseUri = toBaseUri(Uri.parse(url));
//        ConfigItem item = new ConfigItem(WeexDefaultActivity.class, false);
//        WEEX_TABLE.put(baseUri, item);
    }

    synchronized void updateForRouterConfig(JsonData listJson) {
        Map<Uri, ConfigItem> table = new HashMap<>();
        List<JsonData> jsonList = listJson.toList();
        for (JsonData itemJson : jsonList) {
            String url = itemJson.optString("url");
            String className = itemJson.optString("androidClass");
            boolean needLogin = itemJson.optBoolean("needLogin");

            Uri baseUri = toBaseUri(Uri.parse(url));
            if (!isLegalUri(baseUri)) {
                continue;
            }
            Class<? extends Activity> activityClass = ClassUtil.getClassFromName(className, Activity.class);
            if (activityClass == null) {
                continue;
            }

            ConfigItem item = new ConfigItem(activityClass, needLogin);
            table.put(baseUri, item);
        }
        synchronized (TABLE) {
            TABLE.clear();
            TABLE.putAll(table);
        }
    }

    @Nullable
    DeepLinkEntry findEntry(@NonNull Uri uri) {
        Uri baseUri = toBaseUri(uri);
        ConfigItem item = TABLE.get(baseUri);
        if (item != null) {
            return item.toAppEntry(uri);
        }
        if (HttpConfig.WEEX_SCHEME.equals(uri.getScheme())) {
            return findWeexEntry(baseUri, uri);
        }
        if (!TABLE.containsKey(uri) && UriUtil.isNetworkUri(uri)) {
            return findWebEntry(uri);
        }
        return null;
    }

    private static Uri toBaseUri(@NonNull Uri uri) {
        return uri.buildUpon().clearQuery().build();
    }

    private static boolean isLegalUri(Uri uri) {
        return uri != null && !StringUtil.isAnyEmptyAfterTrim(uri.getScheme(), uri.getHost(), uri.getPath());
    }

    boolean isTableEmpty() {
        return TABLE.isEmpty();
    }

    private DeepLinkEntry findWeexEntry(Uri baseUri, Uri uri) {
//        ConfigItem weexItem = WEEX_TABLE.get(baseUri); // temp
//        if (weexItem == null) {
//            if (isNeedLogin(uri)) {
//                weexItem = ConfigItem.DEFAULT_WEEX_ITEM;
//            } else {
//                weexItem = ConfigItem.DEFAULT_WEEX_NOT_NEED_LOGIN_ITEM;
//            }
//        }
//        return weexItem.toWeexEntry(uri);
        return null;
    }

    private DeepLinkEntry findWebEntry(Uri uri) {
        if (isNeedLogin(uri)) {
            return ConfigItem.DEFAULT_WEB_ITEM.toWebEntry(uri);
        } else {
            return ConfigItem.DEFAULT_WEB_NOT_NEED_LOGIN_ITEM.toWebEntry(uri);
        }
    }

    private boolean isNeedLogin(Uri uri) {
        return StringUtil.toBooleanDefaultTrue(uri.getQueryParameter("needLogin"));
    }

    private static class ConfigItem {

//        private static final ConfigItem DEFAULT_WEEX_ITEM = new ConfigItem(WeexDefaultActivity.class, true);
//        private static final ConfigItem DEFAULT_WEEX_NOT_NEED_LOGIN_ITEM = new ConfigItem(WeexDefaultActivity.class, false);
        private static final ConfigItem DEFAULT_WEB_ITEM = new ConfigItem(WebActivity.class, true);
        private static final ConfigItem DEFAULT_WEB_NOT_NEED_LOGIN_ITEM = new ConfigItem(WebActivity.class, false);

        private final Class<? extends Activity> activityClass;
        private final boolean needLogin;

        private ConfigItem(Class<? extends Activity> activityClass, boolean needLogin) {
            this.activityClass = activityClass;
            this.needLogin = needLogin;
        }

        private DeepLinkEntry toAppEntry(Uri uri) {
            return DeepLinkEntry.createForApp(uri, activityClass, needLogin);
        }

        private DeepLinkEntry toWeexEntry(Uri uri) {
            return DeepLinkEntry.createForWeex(uri, activityClass, needLogin);
        }

        private DeepLinkEntry toWebEntry(Uri uri) {
            return DeepLinkEntry.createForWeb(uri, activityClass, needLogin);
        }
    }
}
